Imports System.IO
Imports System.Text

Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Dim CountA, CountMax As Integer
        Dim RandStr As String

        Dim loopa, loopb, loopend, randseedA, randseedB, randseedC, randseedD, stackloop, randseed As Double
        Dim RandWorkA, RandWorkB, randseedE, randseedF, randseedG As Double
        Dim RandMin, RandMax, RandOut As Double
        Dim FILE_NAME As String = "C:\randomtest.txt"

        If System.IO.File.Exists(FILE_NAME) = True Then
            Dim objWriter As New System.IO.StreamWriter(FILE_NAME)
            CountMax = TextBox3.Text

            ProgressBar1.Minimum = 1
            ProgressBar1.Maximum = CountMax
            ProgressBar1.Step = 1
            ProgressBar1.Value = 1 ' Reset Progress Bar at each new run


            ' Begin Loop for number of randoms required

            For CountA = 1 To CountMax
                ProgressBar1.PerformStep()

                Try
                    ' Setup initial random varibles from form and from computer internals

                    RandMin = TextBox1.Text
                    RandMax = TextBox2.Text
                    randseed = TextBox3.Text
                    randseed = ((RandMin + RandMax + randseed) * CountA) / randseed
                    randseed = randseed - Int(randseed)
                    randseedA = My.Computer.Info.TotalPhysicalMemory
                    randseedB = My.Computer.Info.AvailablePhysicalMemory
                    randseedC = My.Computer.Clock.TickCount
                    randseedD = My.Computer.FileSystem.CurrentDirectory.Length
                    randseedE = My.Computer.Clock.TickCount
                    randseedF = My.Computer.Info.AvailableVirtualMemory
                    randseedA = Int(randseedA) + 2
                    randseedB = Int(randseedB) + 2
                    randseedC = Int(randseedC) + 2
                    randseedD = Int(randseedD) + 2

                    'Setup First Loop for randomising

                    loopend = Int(randseedD + 100 + (Rnd(randseedB) * randseedC))
                    loopend = Int(loopend / 1000000.0) + 5

                    For loopa = 1 To loopend
                        randseedE = ((randseedA / loopa) * randseedD) + (randseedE / 2) + (randseed + loopa)
                    Next
                    RandWorkA = (randseedA / randseedB) * randseedC
                    RandWorkA = RandWorkA - Int(RandWorkA)

                    randseedC = My.Computer.Clock.TickCount
                    randseedF = RandWorkA * randseedC + randseedF
                    loopend = Int(randseedC / 10000.0) + 5
                    For loopb = 1 To loopend
                        randseedG = (RandWorkA + randseedF) + randseedG
                        stackloop = loopb

                    Next

                    loopend = Int((randseedE + stackloop) / 10000.0) + 5
                    For loopa = 1 To loopend
                        RandWorkA = randseedA + randseedB
                    Next
                    randseedB = My.Computer.Info.AvailablePhysicalMemory
                    randseedC = My.Computer.Clock.TickCount
                    RandWorkB = randseedC + randseedE + stackloop

                    If RandWorkA > randseedC Then
                        stackloop = (RandWorkA / randseedC) - Int(RandWorkA / randseedC)
                    Else
                        stackloop = (randseedC / RandWorkA) - Int(randseedC / RandWorkA)
                    End If

                    If stackloop > 0.5 Then
                        randseedB = randseedB + randseedC
                    Else
                        randseedB = Math.Abs(randseedB - randseedC)
                    End If

                    ' Finalise and convert to real 0<x>1

                    RandWorkA = (randseedA + randseedG + randseedC + RandWorkA + randseedG) / randseedB
                    RandWorkB = RandWorkB + RandWorkA
                    RandWorkA = RandWorkB - Int(RandWorkB)
                    RandWorkA = Math.Truncate(RandWorkA * 10000000000.0) / 10000000000.0


                    RandOut = (RandWorkA * RandMax) + RandMin

                    ' Check if to convert to interger or not

                    If CheckBox2.Checked = True Then
                        RandOut = Int(RandOut + 0.5)
                        RandStr = RandOut
                    Else
                        RandStr = RandOut.ToString()
                        RandStr = RandStr + "000000000"
                        RandStr = Microsoft.VisualBasic.Left(RandStr, 12)
                    End If

                    ' Write Random Number to File and New Line

                    objWriter.WriteLine(RandStr)

                Catch ex As Exception
                    MsgBox("An error has occured.")

                End Try

            Next

            objWriter.Close()
            MsgBox("Finished")
        End If


    End Sub


End Class
